package com.umeox.watch.moto.contact.model;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.dbflow5.annotation.Column;
import com.dbflow5.annotation.PrimaryKey;
import com.dbflow5.annotation.Table;
import com.dbflow5.contentprovider.annotation.ContentType;
import com.dbflow5.contentprovider.annotation.ContentUri;
import com.dbflow5.contentprovider.annotation.TableEndpoint;
import com.dbflow5.structure.BaseModel;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.AndroidUtils;
import com.umeox.watch.moto.agora.InternalConstants;
import com.umeox.watch.moto.contact.base.BaseDatabase;
import com.umeox.watch.moto.dataservice.DataProvider;

@Table(database = BaseDatabase.class, name = CallLog.TABLE_NAME)
@TableEndpoint(name = CallLog.TABLE_NAME, contentProvider = BaseDatabase.class)
public class CallLog extends BaseModel implements Parcelable {

    public static final String TABLE_NAME = "CallLog";

    @ContentUri(path = CallLog.TABLE_NAME, type = ContentType.VND_MULTIPLE + CallLog.TABLE_NAME)
    public static final Uri CONTENT_URI = BaseDatabase.buildUri(CallLog.TABLE_NAME);

    public static final Creator<CallLog> CREATOR = new Creator<CallLog>() {
        @Override
        public CallLog createFromParcel(Parcel source) {
            return new CallLog(source);
        }

        @Override
        public CallLog[] newArray(int size) {
            return new CallLog[size];
        }
    };
    @NonNull
    @Column
    @PrimaryKey(autoincrement = true)
    private long id;
    @Column
    private String deviceId;
    @Column
    private String friendId;
    @Column
    private long startTime;
    @Column
    private long endTime;
    @Column
    private int type;
    @Column
    private long duration;
    @Column
    private int newCall;

    public CallLog() {
    }

    protected CallLog(Parcel in) {
        this.id = in.readLong();
        this.deviceId = in.readString();
        this.friendId = in.readString();
        this.startTime = in.readLong();
        this.endTime = in.readLong();
        this.type = in.readInt();
        this.duration = in.readLong();
        this.newCall = in.readInt();
    }

    public static void insert(String friendId,
                              long startTime,
                              long endTime,
                              InternalConstants.CallType type) {
        //只记录未接的视频电话和通话时长超过10秒的记录
        boolean result = false, missed = false;
        try {
            CallLog callLog = new CallLog();
            callLog.setDeviceId(AndroidUtils.getDeviceId());
            callLog.setFriendId(friendId);
            callLog.setStartTime(startTime);
            callLog.setEndTime(endTime);
            callLog.setType(type.getType());
            callLog.setDuration(endTime - startTime);
            if (type == InternalConstants.CallType.VIDEO_MISSED_TYPE
                    || type == InternalConstants.CallType.VOIP_MISSED_TYPE) {
                callLog.setNewCall(1);
                missed = true;
            }
            result = callLog.save(BaseDatabase.getDatabase());
            Logger.i("保存通话记录" + callLog + " result " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result && missed) {
            DataProvider.VideoCallProvider.notifyChangeWithAppendedPath(friendId);
        }
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getFriendId() {
        return friendId;
    }

    public void setFriendId(String friendId) {
        this.friendId = friendId;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    @Override
    public String toString() {
        return "CallLog{" +
                "id=" + id +
                ", deviceId='" + deviceId + '\'' +
                ", friendId='" + friendId + '\'' +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", type=" + type +
                ", duration=" + duration +
                ", newCall=" + newCall +
                '}';
    }

    public int getNewCall() {
        return newCall;
    }

    public void setNewCall(int newCall) {
        this.newCall = newCall;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeString(this.deviceId);
        dest.writeString(this.friendId);
        dest.writeLong(this.startTime);
        dest.writeLong(this.endTime);
        dest.writeInt(this.type);
        dest.writeLong(this.duration);
        dest.writeInt(this.newCall);
    }
}
